/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package slic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import slic.model.Equipo;
import slic.model.Puntaje;
import slic.service.PuntajeLocalServiceUtil;
import slic.service.base.PuntajeLocalServiceBaseImpl;
import slic.service.persistence.EquipoUtil;
import slic.service.persistence.PuntajeUtil;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * The implementation of the Puntaje local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link slic.service.PuntajeLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Pablo Carballo
 * @see slic.service.base.PuntajeLocalServiceBaseImpl
 * @see slic.service.PuntajeLocalServiceUtil
 */
public class PuntajeLocalServiceImpl extends PuntajeLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link slic.service.PuntajeLocalServiceUtil} to access the Puntaje local service.
	 */
	
	private static Log _log = LogFactoryUtil.getLog(PuntajeLocalServiceImpl.class);


	private DynamicQuery createDynamicQueryFromMap(Map<String, String> map, boolean count) {

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Puntaje.class);

		try {
			if (map.get("partidoId") != null && !map.get("partidoId").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("partidoId")
						.eq(Long.valueOf(map.get("partidoId").trim())));
		}
		catch (Exception e) {
		}
		


		
		if (!count && map.get("orderBy") != null && !map.get("orderBy").trim().equals("")) {
			if (map.get("orderType") != null && map.get("orderType").trim().equals("desc"))
				query.addOrder(OrderFactoryUtil.desc(map.get("orderBy")));
			else
				query.addOrder(OrderFactoryUtil.asc(map.get("orderBy")));
		}

		return query;

	}

	public List<Puntaje> findByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return PuntajeUtil.findWithDynamicQuery(query);

	}

	public List<Puntaje> findByMap(Map<String, String> map, int start, int end) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return PuntajeUtil.findWithDynamicQuery(query, start, end);

	}

	public long countByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, true);

		return new PuntajeUtil().countWithDynamicQuery(query);

	}

	public Puntaje add(long deportistaId, long equipoId, long partidoId,long pruebaId, long puntos) throws SystemException {

		Puntaje puntaje = PuntajeUtil.create(CounterLocalServiceUtil.increment());
		
		puntaje.setDeportistaId(deportistaId);
		puntaje.setEquipoId(equipoId);
		puntaje.setPartidoId(partidoId);
		puntaje.setPruebaId(pruebaId);
		puntaje.setPuntaje(puntos);
		
		return PuntajeUtil.update(puntaje, true);

	}
	
	

	
}